var arr = [3, 4, 1, 2];
function bubbleSort (arr) {
    for (var j = 0; j < arr.length - 1; j++) {
        // 这里要根据外层for循环的 j，逐渐减少内层 for循环的次数
        for (var i = 0; i < arr.length - 1 - j; i++) {
            if (arr[i] > arr[i + 1]) {
                var temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
    }
    return arr;
}
// bubbleSort(arr);

function quickSort( arr ) {
    if(arr.length <= 1)
        return arr;
    const num = arr[0];
    let left = [], right = [];
    for(let i = 1;i < arr.length; i++) {
        if(arr[i]<=num)
            left.push(arr[i]);
        else
            right.push(arr[i]);
    }
    return quickSort(left).concat([num],quickSort(right));
}

console.log(quickSort(arr));
